package cn.edu.gxu.xingjian.teacherassistant.domain;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Teacher {

    @NotNull
    @Size(min = 2)
    private String name;

    @NotNull
    @Size(min = 2)
    private String age;

    @NotNull
    @Size(min = 4)
    private String workingNumber;

    @NotNull
    @Size(min = 1, max = 1)
    private String sex;

    @NotNull
    @Size(min = 2)
    private String department;

    @NotNull
    @Size(min = 2)
    private String titles;

    @NotNull
    @Size(min = 6)
    private String password;

    @ManyToOne
    private PatternRole patternRole;

    @ManyToOne
    private Faculty faculty;

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

	@Version
    @Column(name = "version")
    private Integer version;

	public Long getId() {
        return this.id;
    }

	public void setId(Long id) {
        this.id = id;
    }

	public Integer getVersion() {
        return this.version;
    }

	public void setVersion(Integer version) {
        this.version = version;
    }

	@PersistenceContext
    transient EntityManager entityManager;

	public static final EntityManager entityManager() {
        EntityManager em = new Teacher().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

	public static long countTeachers() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Teacher o", Long.class).getSingleResult();
    }

	public static List<Teacher> findAllTeachers() {
        return entityManager().createQuery("SELECT o FROM Teacher o", Teacher.class).getResultList();
    }

	public static Teacher findTeacher(Long id) {
        if (id == null) return null;
        return entityManager().find(Teacher.class, id);
    }

	public static List<Teacher> findTeacherEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Teacher o", Teacher.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

	@Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

	@Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Teacher attached = Teacher.findTeacher(this.id);
            this.entityManager.remove(attached);
        }
    }

	@Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

	@Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

	@Transactional
    public Teacher merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Teacher merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

	public String getName() {
        return this.name;
    }

	public void setName(String name) {
        this.name = name;
    }

	public String getAge() {
        return this.age;
    }

	public void setAge(String age) {
        this.age = age;
    }

	public String getWorkingNumber() {
        return this.workingNumber;
    }

	public void setWorkingNumber(String workingNumber) {
        this.workingNumber = workingNumber;
    }

	public String getSex() {
        return this.sex;
    }

	public void setSex(String sex) {
        this.sex = sex;
    }

	public String getDepartment() {
        return this.department;
    }

	public void setDepartment(String department) {
        this.department = department;
    }

	public String getTitles() {
        return this.titles;
    }

	public void setTitles(String titles) {
        this.titles = titles;
    }

	public String getPassword() {
        return this.password;
    }

	public void setPassword(String password) {
        this.password = password;
    }

	public PatternRole getPatternRole() {
        return this.patternRole;
    }

	public void setPatternRole(PatternRole patternRole) {
        this.patternRole = patternRole;
    }

	public Faculty getFaculty() {
        return this.faculty;
    }

	public void setFaculty(Faculty faculty) {
        this.faculty = faculty;
    }
}
